let dao = require('../database/dao'),
  logger = require('../helper/logger'),
  tagLogger = new logger.TagLogger('CRON_MODEL'),
  errorCode = require('../../conf/errorCode'),
  commonModel = require('./commonModel'),
  util = require('../helper/util'),
  _ = require('lodash');

let {
  genUpdateCallback,
  genInsertCallback,
  genUpdateRslt,
  genUpdateBatchRslt,
  genUpdateBatchCallback,
  genSelectCallback,
  genPagination,
  genInertRslt,
} = commonModel;

//获取运营位列表
var deleteExpiredData = (exports.deleteExpiredData = function(
  tableName,
  callback,
) {
  //let  fromJoinWhere = `FROM ${tableName} where ${flag == 1
  //? 'status>=0'
  //: 'status<0'}`;
  //fromJoinWhere = `FROM ${tableName} where status >= 0 and ${flag == 1
  //? 'TO_DAYS(validateDate) >= TO_DAYS(NOW())'
  //: 'TO_DAYS(validateDate) < TO_DAYS(NOW())'}`;
  //let postQuery = {
  //str: `SELECT * ${fromJoinWhere} LIMIT ?,?`,
  //param: [offset, limit],
  //};
  //let countQuery = {
  //str: `SELECT count(1) as count ${fromJoinWhere}`,
  //param: [],
  //};
  //let queryArr = [postQuery, countQuery];
  dao.all(
    `select * from ${tableName} where TO_DAYS(validateDate) < TO_DAYS(NOW()) and status>=0`,
    [],
    callback,
  );
});

//deleteExpiredData('banner', (err, _rel) => {
//for(let i=0,len=_rel.length;i<len;i++){
//let operateItem  = _rel[i],
//prevId = operateItem.prevId,
//nextId = operateItem.nextId;

//}
//});

function promiseify(method) {
  return function(ctx) {
    let args = Array.prototype.slice.call(arguments);

    return new Promise((resolve, reject) => {
      let cb = function(err, result) {
        if (err) {
          return reject(err);
        }

        return resolve(result);
      };

      args.push(cb);
      method.apply(ctx, args);
    });
  };
}

var updateExipredData = async function(tableName) {
  let rel = await promiseify(dao.all)(
    //`select * from ${tableName} where TO_DAYS(validateDate) < TO_DAYS(NOW()) and status>=0`,
    `select * from ${tableName} where validateDate < NOW() and status>=0`,
    [],
  );
  console.log('------------', rel.length);
  for (let i = 0, len = rel.length; i < len; i++) {
    let operateItem = rel[i],
      operateId = operateItem.id,
      prevId = operateItem.prevId,
      nextId = operateItem.nextId;

    let query1 = {
        str: `update ${tableName} set status=-1 where id=?`,
        param: [operateId],
      },
      query2 = {
        str: `update ${tableName} set nextId=? where id=?`,
        param: [nextId, prevId],
      },
      query3 = {
        str: `update ${tableName} set prevId=? where id=?`,
        param: [prevId, nextId],
      },
      queryArray = [];
    queryArray.push(query1);

    if (prevId != -1) {
      queryArray.push(query2);
    }

    if (nextId != -1) {
      queryArray.push(query3);
    }

    let _rel = await promiseify(dao.queryBatch)(queryArray);
    console.log('------');
    console.log(_rel);
  }
};

//let operateTableArray = ['banner', 'news', 'subject', 'topic', 'comment'];

var updateAllOperateDataStatus = (exports.updateAllOperateDataStatus = async function(tableName) {
  //for (let i = 0, len = operateTableArray.length; i < len; i++) {
    let rel = await updateExipredData(tableName);
    console.log('-------');
  //}
});

//updateAllOperateDataStatus();
